package his.controller;

import com.alibaba.fastjson.JSONObject;
import his.entity.Drugs;
import his.entity.Prescription;
import his.service.DrugsService;
import his.vo.Condition;
import his.vo.PrescriptionVO;
import org.example.his.common.api.CommonResponse;
import org.example.his.common.utils.RequestToJsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/drug/")
public class DrugsController {

    @Autowired
    DrugsService drugsService;

    @GetMapping("check_drug/search/{medical_id}")
    @ResponseBody
    public CommonResponse<List<PrescriptionVO>> check_drug(@PathVariable int medical_id)
    {
        return  drugsService.check_drug(medical_id);
    }

    @GetMapping("get_drugs/{casenumber}")
    @ResponseBody
    public CommonResponse<List<PrescriptionVO>> get_drugs_bycasenumber(@PathVariable String casenumber)
    {

        return  drugsService.get_drugs_bycasenumber(casenumber);
    }

    @PostMapping("check_drug/distribute")
    @ResponseBody
    public CommonResponse deliver_drug(HttpServletRequest httpServletRequest)
    {
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        String drugsName=(String) jsonObject.get("DrugsName");
        String prescriptionTime=(String) jsonObject.get("PrescriptionTime");
        return  drugsService.deliver_drug(drugsName,prescriptionTime);
    }

    @PostMapping("drug_return/return")
    @ResponseBody
    public CommonResponse return_drug(HttpServletRequest httpServletRequest)
    {
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        String drugsName=(String) jsonObject.get("DrugsName");
        String prescriptionTime=(String) jsonObject.get("PrescriptionTime");
        return  drugsService.return_drug(drugsName,prescriptionTime);
    }

    @GetMapping("drug_manage/all")
    @ResponseBody
    public CommonResponse<List<Drugs>> getall_drugs()
    {
        return  drugsService.getall_drugs();
    }

    @GetMapping("drug_manage/search/{drug_flag}")
    @ResponseBody
    public CommonResponse<List<Drugs>> getall_drugs_bycodename(@PathVariable String drug_flag)
    {
        return  drugsService.getall_drugs_bycodename(drug_flag);
    }

    @PostMapping("drug_manage/add")
    @ResponseBody
    public CommonResponse add_drugs(HttpServletRequest httpServletRequest)
    {
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        String DrugsCode=(String) jsonObject.get("drugsCode");
        String DrugsName=(String) jsonObject.get("drugsName");
        String DrugsFormat=(String) jsonObject.get("drugsFormat");
        String DrugsUnit=(String) jsonObject.get("drugsUnit");
        BigDecimal DrugsPrice= new BigDecimal((String)jsonObject.get("drugsPrice"));
        int DrugsDosageID=  Integer.parseInt((String)jsonObject.get("drugsDosageID"));
        int DrugsTypeID=  Integer.parseInt((String)jsonObject.get("drugsTypeID"));
        String Manufacturer=(String) jsonObject.get("manufacturer");
        String MnemonicCode=(String) jsonObject.get("mnemonicCode");
        return  drugsService.add_drugs(DrugsCode,DrugsName,DrugsFormat,DrugsUnit,Manufacturer,DrugsDosageID,DrugsTypeID,DrugsPrice,MnemonicCode);
    }

    @PutMapping("drug_manage/modify")
    @ResponseBody
    public CommonResponse modify_drugs(HttpServletRequest httpServletRequest)
    {
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        String DrugsCode=(String) jsonObject.get("drugsCode");
        String DrugsName=(String) jsonObject.get("drugsName");
        String DrugsFormat=(String) jsonObject.get("drugsFormat");
        String DrugsUnit=(String) jsonObject.get("drugsUnit");
        BigDecimal DrugsPrice=(BigDecimal) jsonObject.get("drugsPrice");
        int DrugsDosageID=(int)jsonObject.get("drugsDosageID");
        int DrugsTypeID=(int)jsonObject.get("drugsTypeID");
        String Manufacturer=(String) jsonObject.get("manufacturer");
        String MnemonicCode=(String) jsonObject.get("mnemonicCode");
        return  drugsService.modify_drugs(DrugsCode,DrugsName,DrugsFormat,DrugsUnit,Manufacturer,DrugsDosageID,DrugsTypeID,DrugsPrice,MnemonicCode);
    }

    @DeleteMapping("drug_manage/del/{drug_flag}")
    @ResponseBody
    public CommonResponse delete_drugs(@PathVariable String drug_flag)
    {
        return  drugsService.delete_drugs(drug_flag);
    }

}
